const mysql = require('./../utils/selfDB')

function getStartParams(ctx) {
  var bigtype = ctx.request.body.bigtype;
  var smalltype = ctx.request.body.smalltype;
  var seqid = ctx.request.body.seqid;
  return mysql.select('*').from('system_params')
    .where(function(){
      if (seqid){
        this.where({ bigtype, smalltype, useflag: '0', seqid})
      }else{
        this.where({ bigtype, smalltype, useflag: '0' })
      }
    }
    )
    .orderBy('seqid')
    .then(data => {
      if (data[0].seqid) {
        return {
          status: '0',
          msg: data
        }
      } else {
        return {
          status: '-1',
          msg: 'no data'
        }
      }
    })
    .catch(e => {
      return {
        status: '-1',
        msg: "query error:" + e.message
      }
    })
}

module.exports = (ctx, next) => {
  return getStartParams(ctx).then(result => {
    ctx.state.data = result
  })
}